local qinyin = fk.CreateSkill {

  name = "joy__qinyin",

  tags = {  },

}



qinyin:addEffect(fk.EventPhaseEnd, {
  name = "joy__qinyin",
  events = {fk.EventPhaseEnd},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(qinyin.name) and player.phase == Player.Discard then
      return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from==player and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
        end
        return false
      end, Player.HistoryPhase) > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"loseHp", "joy__qinyin_alldraw"}
    if table.find(room.alive_players, function (p) return p:isWounded() end) then
      table.insert(choices, 1, "recover")
    end
    local choice = room:askForChoice(player, choices, qinyin.name)
    for _, p in ipairs(room:getAlivePlayers()) do
      if not p.dead then
        if choice == "loseHp" then
          room:loseHp(p, 1, qinyin.name)
        elseif choice == "joy__qinyin_alldraw" then
          p:drawCards(1, qinyin.name)
        elseif p:isWounded() then
          room:recover{ who = p, num = 1, recoverBy = player, skillName = qinyin.name }
        end
      end
    end
  end,
})

return qinyin